package com.yunxi.common.config;

import com.google.common.base.Predicate;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leed on 2018/11/6.
 */
@Configuration
@EnableSwagger2
//@Profile({"dev","test"})
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "false")
public class SwaggerConfig {

    private List<Parameter> pars;

    @Bean
    public Docket apiAdmin() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        pars = new ArrayList<>();
        ticketPar.name("x-access-token").description("令牌token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //header中的ticket参数非必填，传空也可以
        pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                if (input.isAnnotatedWith(ApiOperation.class)){
                    if(!input.groupName().startsWith("process")&&
                            !input.groupName().startsWith("task")&&
                            !input.groupName().startsWith("signal")&&
                            !input.groupName().startsWith("execution")&&
                            !input.groupName().startsWith("model")&&
                            !input.groupName().contains("endpoint")&&
                            !input.groupName().contains("resource")
                            )
                    return true;
                }
                return false;
            }
        };

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminAPI")
                .select()
                .apis(predicate)
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiAdminInfo());
    }

    ApiInfo apiAdminInfo() {
        return new ApiInfoBuilder()
                .title("PMS-Admin-Api-Doc")
                .description("PMS api文档")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket api_activiti() {
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                //System.out.println(input.get);
                    if(input.groupName().startsWith("process")||
                            input.groupName().startsWith("task")||
                            input.groupName().startsWith("signal")||
                            input.groupName().startsWith("execution")||
                            input.groupName().startsWith("model")||
                            input.groupName().contains("endpoint")||
                            input.groupName().contains("resource")
                            )
                        return true;
                return false;
            }
        };
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("activitiApi")
                .select()
                .apis(predicate)
                .paths(PathSelectors.any())//过滤的接口
                .build()
                .apiInfo(apiActivitiInfo());
    }

    private ApiInfo apiActivitiInfo() {
        return new ApiInfoBuilder()
                .title("开源工作流")//大标题
                .description("PMS api文档")//详细描述
                .version("1.0")//版本
                .build();
    }

    @Bean
    public Docket api_finance() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/admin/finance/**")).build().groupName("financeDoc").pathMapping("/")
                .apiInfo(apiFinanceInfo());
    }

    ApiInfo apiFinanceInfo() {
        return new ApiInfoBuilder()
                .title("PMS-Finance-资金业务")
                .description("价格类型管理 / 价格管理 / 折扣管理 / 折扣策略管理 / 整车价格录入|审核 / 选装包价格录入|审核")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket api_partner() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/admin/partner/**")).build().groupName("partnerDoc").pathMapping("/")
                .apiInfo(apiPartnerInfo());
    }

    ApiInfo apiPartnerInfo() {
        return new ApiInfoBuilder()
                .title("PMS-Partner-渠道业务")
                .description("渠道信息管理（二网、仓储、发票） / 帐户管理 / 账户组 / 账户组属性 / 帐户类型 / 帐户信用额度 / 业务类型 ")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket api_account() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/admin/account/**")).build().groupName("accountDoc").pathMapping("/")
                .apiInfo(apiAccountInfo());
    }

    ApiInfo apiAccountInfo() {
        return new ApiInfoBuilder()
                .title("PMS-Account-渠道业务")
                .description("账户管理/ 账户组 / 账户组属性 / 帐户类型 / 帐户信用额度  ")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket api_common() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/com/**")).build().groupName("commonDoc").pathMapping("/")
                .apiInfo(apiCommonInfo());
    }

    ApiInfo apiCommonInfo() {
        return new ApiInfoBuilder()
                .title("PMS-common-Api-Doc")
                .description("PMS api文档")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket createOtdRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("OTD API")
            .apiInfo(apiWxInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.yunxi.controller.otd"))//api接口包扫描路径
            .build();
    }
    private ApiInfo apiWxInfo() {
        return new ApiInfoBuilder()
            .title("OTD API")//设置文档的标题
            .description("产销模块API接口")//设置文档的描述->1.Overview
            .version("1.0.0")//设置文档的版本信息-> 1.1 Version information
            .build();
    }

    @Bean
    public Docket createStoRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("STO API")
                .apiInfo(apiStoWxInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yunxi.controller.sto"))//api接口包扫描路径
                .build()
                .globalOperationParameters(pars);
    }
    private ApiInfo apiStoWxInfo() {
        return new ApiInfoBuilder()
                .title("STO API")//设置文档的标题
                .description("仓储API接口")//设置文档的描述->1.Overview
                .version("1.0.0")//设置文档的版本信息-> 1.1 Version information
                .build();
    }

    @Bean
    public Docket createSatRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("SAT API")
                .apiInfo(apiSatWxInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yunxi.controller.sat"))
                .build()
                .globalOperationParameters(pars);
    }
    private ApiInfo apiSatWxInfo() {
        return new ApiInfoBuilder()
                .title("SAT API")//设置文档的标题
                .description("销售助手API接口")//设置文档的描述->1.Overview
                .version("1.0.0")//设置文档的版本信息-> 1.1 Version information
                .build();
    }


    @Bean
    public Docket api_jenkinsTest() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/com/**")).build().groupName("jenkinsTest").pathMapping("/")
                .apiInfo(apiCommonInfo());
    }

    ApiInfo apiJenkinsTestInfo() {
        return new ApiInfoBuilder()
                .title("PMS-jenkinsTest-Api-Doc")
                .description("jenkinsTest 文档")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket createQrtzRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Qrtz API")
                .apiInfo(apiQrtz())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yunxi.controller.qrtz"))//api接口包扫描路径
                .build()
                .globalOperationParameters(pars);
    }
    private ApiInfo apiQrtz() {
        return new ApiInfoBuilder()
                .title("Qrtz API")//设置文档的标题
                .description("Qrtz任务调度API接口")//设置文档的描述->1.Overview
                .version("1.0.0")//设置文档的版本信息-> 1.1 Version information
                .build();
    }

    @Bean
    public Docket createWholesaleRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("wholesale API")
                .apiInfo(apiWholesale())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yunxi.controller.approve"))//api接口包扫描路径

                .build()
                .globalOperationParameters(pars);
    }
    private ApiInfo apiWholesale() {
        return new ApiInfoBuilder()
                .title("wholesale API")//设置文档的标题
                .description("批售API接口")//设置文档的描述->1.Overview
                .version("1.0.0")//设置文档的版本信息-> 1.1 Version information
                .build();
    }
}
